package com.example.testgit.controller;

import com.example.testgit.VO.*;
import com.example.testgit.entity.StudentDO;
import com.example.testgit.service.StudentService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Resource
    private StudentService studentService;

    /**
     * 搜索功能
     * @param stu
     * @return
     */
    @PostMapping("/search")
    public Result<PageResult<StudentVO>> search(@RequestBody StudentQuery stu) {
        PageResult<StudentVO> result = studentService.search(stu.getStuName(), stu.getStuId(),
                                                            stu.getMajor(), stu.getClassNum(),
                                                            stu.getPageNum(), stu.getPageSize());
        return Result.success(result);
    }


    @GetMapping("/basic_infor")
    public Result<StudentBasicVO> basicInfor(Integer stuId){

        StudentBasicVO studentBasicVO = studentService.basicInfor(stuId);

        return Result.success(studentBasicVO);
    }

    @GetMapping("/parent_infor")
    public Result<ParentVO> ParentInfor(Integer stuId){

        ParentVO parentVO = studentService.parentInfor(stuId);

        return Result.success(parentVO);
    }

    /**
     * 上传学生照片
     * @param
     * @return
     */
    @PostMapping("/upload_icon")
    public Result icon(MultipartFile file) throws IOException {
        File dir = new File("D:\\pic");
        if (!dir.exists()) dir.mkdir();
        File savePath = new File("D:\\pic\\" + file.getOriginalFilename());
        file.transferTo(savePath);
        return Result.success(null);
    }

    /**
     * excel表导入
     * @param
     * @return
     */
    @PostMapping("/excel_in")
    public Result excleIn(@RequestParam("file") MultipartFile file) throws IOException {
        studentService.excleIn(file);
        return Result.success(null);
    }

    /**
     * 学生增加
     * @param
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody StudentDO stu){
        if (studentService.insert(stu)) return Result.success(null);
        else return Result.fail();
    }

    /**
     * 学生更新
     * @param
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody StudentDO stu){
        if (studentService.update(stu)) return Result.success(null);
        else return Result.fail();
    }

    /**
     * 学生删除
     * @param
     * @return
     */
    @DeleteMapping("/delete/{stuId}")
    public Result delete(@PathVariable("stuId") Integer stuId){
        if (studentService.delete(stuId)) return Result.success(null);
        else return Result.fail();
    }
}
